import { ethers } from "ethers";

const ALCHEMY_MAINNET_URL = 'https://eth-sepolia.g.alchemy.com/v2/mkM5-GVJCs00ghRsjT7aU';

const provider = new ethers.JsonRpcProvider(ALCHEMY_MAINNET_URL);
// 利用助记词和provider创建钱包
const wallet = ethers.Wallet.fromPhrase('magnet genuine custom runway vessel misery monkey kite chat diary habit decide', provider)


// 利用hd钱包派生出20个钱包地址
const mnemonic = "marble three stumble field evoke old celery air vessel flavor model capable rabbit chalk disease tuition immense olympic liquid tissue stumble undo interest peace"
const hdWallet = ethers.HDNodeWallet.fromPhrase(mnemonic)
const basePath = "44'/60'/0'/0"
let wallets = []
for(let i=0;i<20;i++){
    let hdNodeNew = hdWallet.derivePath(basePath + "/" + i);
    wallets.push(new ethers.Wallet(hdNodeNew.privateKey))
}

// WETH合约
const abiWETH = [
    "function balanceOf(address) public view returns(uint)",
    "function deposit() public payable",
    "function transfer(address, uint) public returns (bool)",
    "function withdraw(uint) public",
    "function approve(address, uint256) public returns (bool)"
];
// WETH合约地址（sepolia 测试网）
const addressWETH = '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9' // WETH Contract
const contractWETH = new ethers.Contract(addressWETH, abiWETH, wallet)

// 先读取其中一个地址的余额
const balanceETH = await provider.getBalance(wallets[10].address)
console.log(`ETH 余额${ethers.formatEther(balanceETH)}`)
const balanceWETH = await contractWETH.balanceOf(wallets[10].address)
console.log(`WETH 余额${ethers.formatEther(balanceWETH)}`)


// 归集ETH
// const sendParam = {
//     to: wallet.getAddress(),
//     value: ethers.parseEther('0.00009') 
// }
// for(let i =0 ; i< 20; i++){
//     // 钱包链接节点才能发送交易
//     let walletWithProvide = wallets[i].connect(provider)
//     var tx = await walletWithProvide.sendTransaction(sendParam)
// }
// await tx.wait()
// console.log(tx)
// console.log(`ETH 归集结束`)

// 归集WETH
// for(let i =0 ; i< 20; i++){
//     // 钱包链接节点才能发送交易
//     let walletWithProvide = wallets[i].connect(provider)
//     // 合约连上新的钱包
//     let contractNew = contractWETH.connect(walletWithProvide)
//     // 调用合约的transfer转账
//     var tx = await contractNew.transfer(wallet.getAddress(), ethers.parseEther('0.00009'))
// }
// await tx.wait()
// console.log(tx)
// console.log(`WETH 归集结束`)
